package com.yx.springai.chatclient.configuration;

import com.alibaba.cloud.ai.memory.redis.RedisChatMemoryRepository;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyChatMemoryConfiguration {

    @Value("${spring.ai.memory.redis.host}")
    private String redisHost;
    @Value("${spring.ai.memory.redis.port}")
    private Integer redisPort;
    @Value("${spring.ai.memory.redis.password}")
    private String redisPassword;
    @Value("${spring.ai.memory.redis.timeout}")
    private Integer redisTimeout;

    /**
     * jdbc 保存对话记录
     *
     */
//    @Bean
//    ChatMemory jdbcChatMemory(JdbcChatMemoryRepository chatMemoryRepository){
//        return MessageWindowChatMemory.builder().maxMessages(10).chatMemoryRepository(chatMemoryRepository).build();
//    }

    /**
     * redis 保存对话记录
     * @param
     * @return
     */
    @Bean
    ChatMemory redisChatMemory(RedisChatMemoryRepository chatMemoryRepository){
        return MessageWindowChatMemory.builder().maxMessages(10).chatMemoryRepository(chatMemoryRepository).build();
    }
    
    /**
     * redis 保存对话记录
     * @param
     * @return
     */
    @Bean
    RedisChatMemoryRepository redisChatMemoryRepository(){
        return RedisChatMemoryRepository
                .builder()
                .host(redisHost)
                .port(redisPort)
                .password(redisPassword)
                .timeout(redisTimeout)
                .build();
    }
}